﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using Parallel3M.parallel3m.payload;


namespace Parallel3M.parallel3m.framework
{
    public class ParallelApplication
    {
        public string pcsid;
        public string name;
        public string config;
        public string appPath;
        public int NumberOfCores = 1;
        public bool enable = false;

        public Object ownerApp;
        
        public Hashtable elements=new Hashtable();
        public Hashtable connections=new Hashtable();

        public ParallelChannel userSingleTaskChannel;
        public ParallelChannel userGraphTaskChannel;
        public ParallelSinker userCompletedSinker;

        //user interface
        public void AddUserPayload(ParallelPayload payload)
        {
            if (payload is PayloadGraph)
            {
                userGraphTaskChannel.AddPayload(payload);
            }
            else
            {
                userSingleTaskChannel.AddPayload(payload);
            }
        }

        public bool isCompletedUserPayload(String payload_id)
        {
            return false;
        }

        public ParallelPayload getCompletedUserPayload(String payload_id)
        {
            return null;
        }
    
        //
        public void addParallelElement(ParallelElement element)
        {
            elements.Add(element.name, element);
        }
        public void addParallelConnection(ParallelConnection connnection)
        {
            connections.Add(connnection.name, connnection);
        }


        public void LoadConfigXmlFile(String filename)
        {
            String configxml = File.ReadAllText(filename);
            init(configxml);
        }

        public void init(string configxml)
        {
            ParallelApplicationConfigParser parser = new ParallelApplicationConfigParser();
            parser.application = this;
            parser.Parser(configxml);
            
            config = configxml;
            
            /*example the parallel computing
            ParallelProcessor p1 = new ParallelSource();
            p1.id = Guid.NewGuid().ToString();
            p1.name = "P1";
            p1.runtype = PROCESSORCONSTANT.RUNTYPE_TASK;
            addParallelElement(p1);

            ParallelChannel channel1 = new ParallelChannel();
            channel1.name = "C1";
            addParallelElement(channel1);

            ParallelSinker p2 = new ParallelSinker();
            p2.id = Guid.NewGuid().ToString();
            p2.name = "P2";
            p2.runtype = PROCESSORCONSTANT.RUNTYPE_THREAD;
            addParallelElement(p2);


            ParallelSinker p3 = new ParallelSinker();
            p3.id = Guid.NewGuid().ToString();
            p3.name = "P3";
            p3.runtype = PROCESSORCONSTANT.RUNTYPE_THREAD;
            addParallelElement(p3);

            ParallelConnection conn1 = new ParallelConnection();
            conn1.name = "conn1";
            conn1.from = p1;
            conn1.to = channel1;
            addParallelConnection(conn1);

            ParallelConnection conn2 = new ParallelConnection();
            conn2.name = "conn2";
            conn2.from = channel1;
            conn2.to = p2;
            addParallelConnection(conn2);

            ParallelConnection conn3 = new ParallelConnection();
            conn3.name = "conn3";
            conn3.from = channel1;
            conn3.to = p3;
            addParallelConnection(conn3);


            p1.outputConnections.Add(conn1);
            channel1.inputConnections.Add(conn1);
            channel1.outputConnections.Add(conn2);
            channel1.outputConnections.Add(conn3);
            p2.inputConnections.Add(conn2);
            p3.inputConnections.Add(conn3);
             * 
             */ 
           
        }



        public void start()
        {
            foreach (DictionaryEntry element in elements)
            {
                if (element.Value is ParallelProcessor)
                {
                    (element.Value as ParallelProcessor).start();
                }
            }
        }

        public void stop()
        {
            foreach (DictionaryEntry element in elements)
            if (element.Value is ParallelProcessor)
            {
                (element.Value as ParallelProcessor).stop();
            }
        }
    }
}
